import { AxiosRequestConfig } from "axios";

import apiHandler, {
  reqPendingPayload,
  reqFulfilledPayload,
  reqRejectedPayload,
} from "./apiHandler";

type PayloadActionTypes = {
  status?: string;
  data?: null | any;
  error?: null | any;
  isLoading?: boolean;
};

export default (
  type: any,
  config: AxiosRequestConfig | any,
  data?: any
) => async (
  dispatch: (arg0: { type: string; payload: PayloadActionTypes }) => void
) => {
  dispatch(type(reqPendingPayload));
  await apiHandler({ ...config, data })
    .then((res) => dispatch(type(reqFulfilledPayload(res))))
    .catch((error) => dispatch(type(reqRejectedPayload(error))));
};
